package com.kyzh.core.pager.weal.share

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import com.blankj.utilcode.util.KeyboardUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.gushenge.core.beans.ShareHistory
import com.gushenge.core.requests.WealRequest
import com.gushenge.core.toast
import com.kyzh.core.R
import com.gushenge.core.base.activity.BaseActivity
import com.kyzh.core.databinding.ActivityShareHistorynewBinding
import com.kyzh.core.databinding.DialogShareSendBinding
import com.kyzh.core.databinding.ItemShareNewBinding
import kotlinx.android.synthetic.main.activity_share_history.*
import java.math.BigDecimal

class ShareHistoryNewActivity : BaseActivity() {
    companion object{
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context,ShareHistoryNewActivity ::class.java)
            context.startActivity(starter)
        }
    }
    private var mp = 1
    private lateinit var db : ActivityShareHistorynewBinding
    private val adapter = Adapter()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        db = DataBindingUtil.setContentView(this,R.layout.activity_share_historynew)
        db.rev.adapter = adapter
        adapter.setEmptyView(R.layout.empty)

        KeyboardUtils.fixAndroidBug5497(this@ShareHistoryNewActivity)
        KeyboardUtils.clickBlankArea2HideSoftInput()

        initData()
        db.sendRecord.setOnClickListener {
            ShareRecordActivity.start(this)
        }
        adapter.loadMoreModule.setOnLoadMoreListener {
            mp++
            WealRequest.shareHistory(0,mp){p , max , beans ->
                adapter.addData(beans)
                adapter.loadMoreModule.loadMoreComplete()
                if(p >= max){
                    adapter.loadMoreModule.loadMoreEnd(true)
                }
            }
        }
        adapter.setOnItemChildClickListener{a ,v,p->
            if(v.id ==R.id.imgSend ){
                val bottomSheetDialog = BottomSheetDialog(this, R.style.dialogSoftInput1)

                val inflate = DialogShareSendBinding.inflate(layoutInflater)
                inflate.apply {
                    KeyboardUtils.showSoftInput(tvMoney)
                    tvSure.setOnClickListener {
                        val data = adapter.data[p]
                        val money = tvMoney.text.toString().trim()
                        if(!money.isNullOrBlank()){
                            val toDouble = money.toDouble()
                            if(toDouble==0.00){
                                "请输入正确金额1".toast()
                            }else{
                                WealRequest.sendShareGift(data.user_id,tvMoney.text.toString().trim()){
                                    mp = 1
                                    initData()
                                }
                            }
                        }else{
                            "请输入正确金额".toast()
                        }

                    }
                    tvCancel.setOnClickListener {
                        bottomSheetDialog.dismiss()
                    }
                    ivSub.setOnClickListener {
                        val trim = tvMoney.text.toString().trim()
                        if(trim.isNullOrBlank()){
                            return@setOnClickListener
                        }
                        val toDouble = trim.toDouble()
                        if(toDouble<=0){
                            "请输入正确金额".toast()
                            return@setOnClickListener
                        }
                        tvMoney.setText(BigDecimal(trim).subtract( BigDecimal("0.1")).toString())
                    }
                    ivAdd.setOnClickListener {
                        var trim = tvMoney.text.toString().trim()
                        if(trim.isNullOrBlank()){
                            trim = "0"
                        }
                        tvMoney.setText(BigDecimal(trim).add( BigDecimal("0.1")).toString())
                    }
                }
                bottomSheetDialog.setContentView(inflate.root)
                bottomSheetDialog.show()
            }
        }

    }
    private fun initData(){
        WealRequest.shareHistory(0,mp){p , max , beans ->
            adapter.setNewInstance(beans)
            if(p >= max){
                adapter.loadMoreModule.loadMoreEnd(true)
            }
        }
        WealRequest.getShareInfo {
            db.data = this
        }
    }
    private inner class Adapter:BaseQuickAdapter<ShareHistory,BaseDataBindingHolder<ItemShareNewBinding>>(R.layout.item_share_new),LoadMoreModule{
        init {
            addChildClickViewIds(R.id.imgSend)
        }
        override fun convert(
            holder: BaseDataBindingHolder<ItemShareNewBinding>,
            item: ShareHistory
        ) {

            holder.dataBinding?.apply {
                data = item
            }
        }

    }
}